let App = getApp();
Page({
  data: {
    value: '',
    checked: false,
    refresh:false,
    down:false,
    orderList:[],
    allData:true,
    notData:false,
    page:1,
    pagesize:10,
    getData:""
  },
  //修改自忙
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
    let buys = ''
    if(detail){
      buys = 1
    }else{
      buys = 0
    }
    App._post(App.Domain+"Handler/Applet/changebusy",{isbusy:buys},(res)=>{
      if(res.code == 1){
         if(buys == 1){
          wx.showToast({
            title: '开启自忙',
            icon: '',
            duration: 2000
          })
         }else{
          wx.showToast({
            title: '关闭自忙',
            icon: 'success',
            duration: 2000
          })
         }
      }else{
        wx.showToast({
          title: res.data.info,
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  //去处理
  handle(e){
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../orderReturn/index?id='+id
    })
  },
  onLoad: function (options) {
    this.setData({
      page:1
     })
    let that = this
    App._get(App.Domain+'Handler/Applet/getUserInfo', {}, function (result) {
      if(result.code == 1){
        if(result.data.is_busy == 1){
          that.setData({
            checked:true
          })
        }else{
          that.setData({
            checked:false
          })
        }
      }
    })
    //获取订单列表
    this.getList()
  },
  onShow() {
   
  },
  getList(isPage){
    let that = this
    let data = {
      page:that.data.page,
      pagesize:that.data.pagesize
    }
    App._get(App.Domain+'Handler/Applet/getOrderNotDeal', data, function (result) {
      if(result.code == 1){
        if(result.data.list.length<10){
         that.setData({
            allData:false,
            notData:true
         })
        }
        if(that.data.page == 1){
          that.setData({
            orderList: result.data.list
          })
        }else{
          that.setData({
            orderList:that.data.orderList.concat(result.data.list)
          })
        }
      }
    });
  },
  //下拉刷新
  onPullDownRefresh() {
    this.setData({refresh:true,allData:true,page:1, notData:false})
    this.getList()
    wx.showNavigationBarLoading() 
    setTimeout(()=>{
      this.getData = '数据拿到了'
      this.setData({refresh:false})
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    },500)
  },
  //触底加载
  onReachBottom:function(options){
    let that = this
    if(!this.data.allData){
      return
    }else{
      this.setData({
        down:true
      })
      let page = that.data.page
      page++
      this.setData({page:page})
      setTimeout(()=>{
        that.getList()
        this.getData = '数据拿到了'
        this.setData({down:false})
        wx.stopPullDownRefresh()
        wx.hideNavigationBarLoading()
      },1000)
     
    }
  }
})